import dayjs from 'dayjs';
import { TARGET_TYPE } from '../constants/common';
import { LANDING_PAGE, PACKAGE_ACTIVITY_PAGE } from '../constants/route';
import { setEmptyObj, getQRcodeScene } from '../common/utils';

/**
 * 生成可选年份、月份的数据
 * @param {Number} startYear 开始年份
 * @param {Number} startMonth 开始月份
 */
export const createMonthColumns = (startYear, startMonth = 1) => {
    let monthsColumn = {};
    for (let i = startYear; i <= dayjs().year(); i++) {
        let month = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
        if (i === startYear) month = month.filter(item => item >= startMonth);
        monthsColumn = {
            ...monthsColumn,
            [i]: month,
        };
    }
    return monthsColumn;
};

/**
 * 处理页面进入页面追踪参数
 * @param {String} path 
 * @param {Object} query 
 */
export const dealLandingPage = (path, query) => {
    const { brandId, storeId, owner, a, b } = Object.assign(query, getQRcodeScene(query));
    let page = LANDING_PAGE[path] || 'other';
    if (path && PACKAGE_ACTIVITY_PAGE['detail'].indexOf(path) > -1) {
        if (owner === TARGET_TYPE.brand) {
            page = page[0];
        } else if (owner === TARGET_TYPE.spokesman) {
            page = page[1];
        } else if (owner === TARGET_TYPE.store) {
            page = page[2];
        } else {
            page = page[0];
        }
    }
    return setEmptyObj({ page, brandId, storeId, a, b });
};

/**
 * 获取月份的开始和结束，时间戳
 * @param {Number} year 年份
 * @param {Number} month 月份
 */
export const getDateMonthStartAndEnd = (year, month) => {
    const nowDate = dayjs();
    const _year = year || nowDate.year();
    const _month = nowDate.month();
    const start = new Date(_year, month ? month - 1 : _month, 1);
    const end = new Date(_year, month || _month + 1, 0);
    const _start = dayjs(start).format('YYYY-MM-DD');
    const _end = dayjs(end).format('YYYY-MM-DD');
    return {
        startTime: dayjs(`${_start} 00:00:00`).valueOf(),
        endTime: dayjs(`${_end} 23:59:59`).valueOf(),
    };
};
